package com.example.shortmessage.ui.fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.shortmessage.adapter.MyMessageAdapter;
import com.example.shortmessage.base.BaseFragment;
import com.example.shortmessage.contract.MessageContract;
import com.example.shortmessage.data.entity.MessageData;
import com.example.shortmessage.databinding.FragmentMessageBinding;
import com.example.shortmessage.presenter.MessagePresenter;

import java.util.ArrayList;
import java.util.List;

public class MessageFragment extends BaseFragment<FragmentMessageBinding, MessageContract.View<MessageContract.Presenter>,MessageContract.Presenter>
        implements MessageContract.View<MessageContract.Presenter> {

    private  List<MessageData> messageDataLists;
    private  List<MessageData> messageDataListClone = new ArrayList<>();
    private MyMessageAdapter myMessageAdapter;

    public MessageFragment(List<MessageData> messageDataList) {
        messageDataLists = messageDataList;
    }


    @Override
    protected MessageContract.Presenter initPresenter() {
        return new MessagePresenter(context);
    }

    @Override
    protected FragmentMessageBinding initLayout(LayoutInflater inflater, ViewGroup container) {
        return FragmentMessageBinding.inflate(inflater,container,false);
    }

    @Override
    protected void initView() {
        myMessageAdapter = new MyMessageAdapter(messageDataListClone,getActivity(),mPresenter);
        data();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        mBinding.evContent.setLayoutManager(linearLayoutManager);
        mBinding.evContent.setAdapter(myMessageAdapter);
        mBinding.btnSeach.setOnClickListener(v -> {
            data();
        });
    }

    private void data(){
        messageDataListClone.clear();
        String string = mBinding.etText.getText().toString().trim();
        for (MessageData messageData:messageDataLists){
            if(string.equals("")||messageData.getBody().contains(string)||(messageData.getNumber() != null&&messageData.getNumber().contains(string))
                    ||(messageData.getName() != null && messageData.getName().contains(string))){
                messageDataListClone.add(messageData);
            }
        }
        myMessageAdapter.notifyDataSetChanged();

    }

    @Override
    public void onResume() {
        mBinding.etText.setText("");
        data();
        super.onResume();
    }

    @Override
    public void setPresenter(@NonNull MessageContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {
        Log.e(TAG, "onError: ",throwable );
    }
}
